﻿using Microsoft.Phone.Controls;
using ReservationChecker.ViewModels;
using System;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace ReservationChecker
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the LongListSelector control to the DailySummary data
            DataContext = App.ViewModel;
            
        }

        // Load data for the ViewModel Items
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            App.ViewModel.IsDataLoaded = false;                         // sets IsDataLoaded to false to allow it to reload queries using the back button
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();               
            }
        }

        // Handle selection changed on LongListSelector
        private void MainLongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected item is null (no selection) do nothing
            if (MainLongListSelector.SelectedItem == null)
                return;

            // Navigate to the new page and passes in the Date selected (from the LineOne binding)
            NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItem=" + (MainLongListSelector.SelectedItem as ItemViewModel).LineOne, UriKind.Relative));
            
            // Reset selected item to null (no selection)
            MainLongListSelector.SelectedItem = null;
        }
    }
}